package com.lyy.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyy.Vo.DeptVo;
import com.lyy.aspect.ControllerEndpoint;
import com.lyy.handler.BusinessException;
import com.lyy.response.Result;
import com.lyy.response.ResultCode;
import com.lyy.system.entity.Department;
import com.lyy.system.service.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lyy
 * @since 2022-05-15
 */
@Api(value = "部门管理",tags = "部门管理接口")
@RestController
@RequestMapping("/department")
@CrossOrigin
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;


    @ApiOperation(value = "查询部门人数",notes = "从部门表中分组查询用户人数")
    @GetMapping("/findDeptAndCount")
    public Result findDeptAndCount(){
        List<Department> departments=departmentService.findDeptAndCount();
        if (departments.size()==0){
            throw new BusinessException(ResultCode.DEPARTMENT_NOT_EXIST.getCode(),
                    ResultCode.DEPARTMENT_NOT_EXIST.getMessage());
        }
        return Result.ok().data("departments",departments);
    }

    /**
     * 条件分页查询
     * @param current
     * @param size
     * @param deptVo
     * @return
     */
    @PostMapping("/findDeptPage")
    @ApiOperation(value = "条件分页查询部门列表",notes = "条件分页查询")
    public Result findDeptPage(@RequestParam(required = true,defaultValue = "1") Integer current,
                               @RequestParam(required = true,defaultValue = "6") Integer size,
                               @RequestBody DeptVo deptVo){
        Page<Department> page =new Page<>(current,size);
        QueryWrapper<Department> wrapper =new QueryWrapper<>();
        if (deptVo!=null){
            if (!StringUtils.isEmpty(deptVo.getName())){
                wrapper.eq("name",deptVo.getName());
            }
            if (!StringUtils.isEmpty(deptVo.getPhone())){
                wrapper.eq("phone",deptVo.getPhone());
            }
        }
        IPage<Department> departmentIPage = departmentService.findDeptPage(page,wrapper);
        Long total =departmentIPage.getTotal();
        List<Department> records =departmentIPage.getRecords();
        return Result.ok().data("total",total).data("records",records);
    }

    @ControllerEndpoint(exceptionMessage = "添加部门失败", operation = "添加部门")
    @ApiOperation(value = "添加部门",notes = "添加部门信息")
    @PostMapping("/addDept")
    public Result addDept(@RequestBody Department department){
        departmentService.addDept(department);
        return Result.ok();
    }

    @ControllerEndpoint(exceptionMessage = "更新部门失败", operation = "更新部门")
    @ApiOperation(value = "更新部门",notes = "更新部门信息")
    @PostMapping("/updataDepat")
    public Result updataDepat(@RequestBody Department department){
        departmentService.updataDepat(department);
        return Result.ok();
    }

    @ControllerEndpoint(exceptionMessage = "删除部门失败", operation = "删除部门")
    @ApiOperation(value = "删除部门",notes = "删除部门信息")
    @DeleteMapping("/deleteDeprById")
    public Result deleteDeprById(Long id){
        departmentService.deleteDeprById(id);
        return Result.ok();
    }
}

